package edu.stthomas.seis752.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import edu.stthomas.seis752.dao.UserDao;
import edu.stthomas.seis752.model.Collection;
import edu.stthomas.seis752.model.CollectionType;
import edu.stthomas.seis752.model.Media;
import edu.stthomas.seis752.model.Member;
import edu.stthomas.seis752.model.User;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
    private UserDao userDao;
	
	@Autowired
	private MediaService mediaService;
	
	@Transactional
	public void addUser(User user, MultipartFile profilePic) {
		userDao.addUser(user);
		
		if(profilePic != null){
        	Media profileMedia = mediaService.uploadMedia(profilePic, user, "profilePic");
        	user.setProfilePicture(profileMedia);
        }

	}
	
	@Transactional
	public User getUser(Long id) {
		return userDao.getUser(id);
	}

	@Override
	@Transactional
	public List<User> getUsers(int number) {
		// TODO Auto-generated method stub
		return userDao.getUsers(number);
	}

	@Override
	@Transactional
	public List<Collection> getCollectionsForOwner(Long userId) {
		return userDao.getCollectionsForOwner(userId);
	}

	@Override
	@Transactional
	public CollectionType getCollectionType(String code) {
		return userDao.getCollectionType(code);
	}

	@Transactional
	public Collection addCollection(Collection coll) {
		return userDao.addCollection(coll);
	}

	@Transactional
	public void saveCollection(Collection coll) {
		userDao.saveCollection(coll);
	}
	
	@Transactional
	public void saveMember(Member m){
		userDao.saveMember(m);
	}
	
	@Transactional
	public void remove(Object o){
		userDao.remove(o);
	}
	
	@Transactional
	public Object getPersistedObject(Class c, Object id){
		return userDao.getPersistedObject(c, id);
	}

    @Override
    @Transactional
    public User getUserByUserName(String userName) {
        return userDao.findByUsername(userName);
    }

}
